#!/usr/bin/perl # useful for syntax highlighting.
package clreqd_modules;

use cldb;
use pidlock;
use C_client2;
use Sys::Hostname;

#########################################################################
# This utility is used to stop a daemon. It requires a DAEMON argument	#
# in the form: [lha_]daemon[d],...					#
# hb,stat,lock,cluster,sync and lock are accepted.			#
#									#
# It attempts to send the daemon an RESTART message though if it does	#
# not get a response it will attempt signals - though that is not	#
# implemented as yet. The TIMEOUT and SIGALS are optional:		#
#									#
# TIMEOUT - number of seconds to wait and return error if not aborted.	#
# SIGNALS - comma separated list of signals to use to kill daemon -	#
#		  not implemented as yet.				#
#									#
# Output is:								#
# daemon OK								#
# or:									#
# daemon FAIL msg							#
# ERROR msg								#
#									#
#########################################################################

sub daemon_restart {
my %ARGS=@_;
my @D=qw(hbd lockd netd lockd syncd statd mond clusterd);
my $timeout=3;

	if(exists($ARGS{TIMEOUT})) {
		$timeout=$ARGS{TIMEOUT};
	}

	my $d=$ARGS{DAEMON};
	my @qu=split(/,/,$d);
	
	for (@qu) {
		s/^lha_//;
		$_.="d" if ! /d$/;
	}
	for $d (@qu) {
		if(!scalar(grep {/^$d$/} @D)) {
			return "FAIL Daemon '$d' is unknown.";
		}
	}
	my $cldb=undef;

	my @r=();
	for $d (@qu) {
		#################################################
		# No lock, then check PING request, just 	#
		# in case no lock file was created.		#
		#################################################
		$cldb=new cldb("$::ENV{TRUECL_CFG}/config.db");
		if(!defined($cldb)) {
			::error("Unable to open database:\n" . cldb::error);
		}
		my ($client,$port);
		my $h=$cldb->get_rows("SELECT number FROM ports WHERE detail = 'lha_$d';");
		if(!defined($h) || !defined($h->[0])) {
			return "ERROR Port not found for 'lha_$d' in config database.";
		}
		$cldb->disconnect();
		$port=$h->[0]->{number};
		$client=new C_client2(SERVER_NAME => hostname,SERVER_INET => "127.0.0.1",
				SERVER_PORT=>$port, PKI=>$::PKI);
		if(!defined($client)) {
			return "FAIL Unable to gain connection to 'lha_$d'.";
		}
		$client->set_params(TIMEOUT => $timeout);

		#################################################################
		# We have a connection, so send a message via it and wait for 	#
		# the response.							#
		#################################################################

		my ($resp,$err)=$client->send_request("RESTART");
		if(defined($resp) && $resp =~ /OK/) {
			push @r,"$d\tOK";
			next;
		} 
		if(defined($err) && $err =~ /^CONNECT ERROR:/) {
			push @r,"$d\tERROR: Daemon not running"; # daemon down
			next;
		}
		if(defined($resp) && $resp =~ /^UNKNOWN_REQUEST/) {
			push @r,"$d\tERROR: Daemon not recognised 'restart' request."; # daemon down
			next;
		}
		if(defined($resp)) {
			push @r,"$d\tERROR: Unexpected response: $resp";
			next;
		}
		push @r,"$d\tFAIL $err" if defined($err);
	}
	return "OK\n".join("\n",@r);
}

1;
